package io.osw.modules.sys.service.impl;

import io.osw.common.utils.Constant;
import io.osw.modules.sys.entity.TblCompanyEntity;
import io.osw.modules.sys.entity.TblMtlItemEntity;
import io.osw.modules.sys.service.TblCompanyService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.osw.common.utils.PageUtils;
import io.osw.common.utils.Query;

import io.osw.modules.sys.dao.TblBudgetCalendarDao;
import io.osw.modules.sys.entity.TblBudgetCalendarEntity;
import io.osw.modules.sys.service.TblBudgetCalendarService;


@Service("tblBudgetCalendarService")
public class TblBudgetCalendarServiceImpl extends ServiceImpl<TblBudgetCalendarDao, TblBudgetCalendarEntity> implements TblBudgetCalendarService {

    @Autowired
    private TblCompanyService tblCompanyService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String periodName = (String)params.get("periodName");
        Page<TblBudgetCalendarEntity> page = this.selectPage(
                new Query<TblBudgetCalendarEntity>(params).getPage(),
                new EntityWrapper<TblBudgetCalendarEntity>()
                        .like(StringUtils.isNotBlank(periodName),"period_name", periodName)
                        .addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );
        List<TblBudgetCalendarEntity> records = page.getRecords();

        for (TblBudgetCalendarEntity record : records) {
            if (record == null) {
                return null;
            }

            //注入公司名
            TblCompanyEntity tblCompanyEntity = tblCompanyService.selectById(record.getCompanyId());
            record.setCompanyName(tblCompanyEntity.getCompanyName());


        }


        return new PageUtils(page);
    }

}
